package modulesAPI.v2.composite;

import java.util.ArrayList;

import warehouse.Gazebo;
import warehouse.Table;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCheckBox;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.basics.ModIntTextField;

import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

public class MGazebo extends Module<Gazebo>{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un gazebo
	 * */
	public MGazebo(Module<?> ownerModule, Gazebo content, String tag,String description, ViewType viewType,int... options) {
		super(ownerModule, content, tag, description, viewType,options);
		this.createModules();
		this.addModules(number, partecipant, disponibility, tables);
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un gazebo
	 * */
	public MGazebo(Module<?> ownerModule, String tag,String description, ViewType viewType) {
		super(ownerModule, new Gazebo(true, 30, 1), tag, description, viewType);
		this.createModules();
		this.addModules(number, partecipant, disponibility, tables);
	}
	
	private void createModules(){
		this.partecipant = new ModIntTextField(this, this.getContent().getPartecipantCapacity(), "PARTECIPANT_CAPACITY", "PARTECIPANT_CAPACITY_DESC", this.getViewType());
		this.number = new ModIntTextField(this, this.getContent().getGazeboNumber(), "GAZEBO_NUMBER", "GAZEBO_NUMBER_DESC", this.getViewType());
		this.disponibility =new ModCheckBox(this, this.getContent().getDisponibility(), "DISPONIBILITY", "DISPONIBILITY_DESC", this.getViewType());
		ArrayList<Module<?>> tab = new ArrayList<Module<?>>();
		for(Table t: this.getContent().getTables())
			tab.add(new MTable(this, t, "TABLE", "TABLE_DESC", this.getViewType()));
		tables = new ModCollectionViewer(this, tab, "GAZEBO_TABLES", "GAZEBO_TABLES_DESC", this.getViewType());
		tables.addModuleChangedListener(this);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(this.getContent().getGazeboNumber()+"  "+CWords.getWord("PARTECIPANT_CAPACITY")+": "+this.getContent().getPartecipantCapacity(), this.getContent().getDisponibility()?CWords.getWord("DISPONIBLE"):CWords.getWord("NOT_DISPONIBLE"), isSelected);
	}
	protected void beforeFireChange(){
		this.setContent(new Gazebo(
				this.getContent().getId(), 
				disponibility.getContent(),
				partecipant.getContent(),
				number.getContent()
				));
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {
		
	}

	@Override
	protected void afterFireChange(Module<?> mod) {
		
	}
	private ModCheckBox disponibility;
	private ModIntTextField partecipant;
	private ModIntTextField number;
	private ModCollectionViewer tables;
}
